//
//  ConInformationController.m
//  Shengzhuli
//
//  Created by FXGuo on 17/3/19.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConInformationController.h"
#import "InfomationCell.h"
#import "SDCycleScrollView.h"
#import "ActivityNetService.h"
#import "MBProgressHUD+MJ.h"
#import <MJRefresh.h>
#import "ConActivityListController.h"
#import "ConInfomationDetailController.h"
#import "BannerModel.h"
#import "ConOldActivityDetailController.h"
#import "ClubNetwork.h"

@interface ConInformationController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak) UITableView *tableView;
@property (nonatomic,assign) NSInteger curPage;
@property (nonatomic,strong) NSMutableArray *infomations;

@end

@implementation ConInformationController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setTableViews];
    self.curPage = 1;
    self.title = @"动态";
    [self.tableView.mj_header beginRefreshing];
}

- (void)setTableViews
{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW,ScreenH-SafeAreaTopHeight)];
    tableView.backgroundColor = [UIColor colorWithHexString:@"#EFEFF4"];
    tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 0);
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.rowHeight = 32+(ScreenW*230/750)*146/230;
    tableView.tableFooterView = [[UIView alloc]init];
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([InfomationCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([InfomationCell class])];
    [self.view addSubview:tableView];
    tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.curPage = 1;
        [self getInfomationList];
    }];
    tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.curPage++;
        [self getInfomationList];
    }];
    self.tableView = tableView;
}
#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc]init];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.infomations.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InfomationCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([InfomationCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.oldModel = self.infomations[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ConOldActivityDetailController *detailVc = [[ConOldActivityDetailController alloc]init];
    InfomationModel *model = self.infomations[indexPath.row];
    detailVc.ID = model.ID;
    if (model.videoUrl.length > 0) {
        detailVc.videoURL = [NSURL URLWithString:model.videoUrl];
        detailVc.videoName = model.videoName;
        detailVc.avatarUrl = model.avatarUrl;
    }
    [self.navigationController pushViewController:detailVc animated:YES];
}
- (void)getInfomationList
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"pageNo"] = @(self.curPage);
    [self showWaiter];
    [ClubNetwork getOldActivityListWithParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        [self hideReloadView];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (success) {
            [self hideRequestAgainView];
            self.tableView.mj_footer.hidden = lists.count<20;
            if (self.curPage == 1) {
                [self.infomations removeAllObjects];
            }
            [self.infomations addObjectsFromArray:lists];
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
    
//    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
//    parame[@"pageNo"] = @(self.curPage);
//    [self showClearBgWaiter];
//    [ActivityNetService getInformationListWithParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
//        [self hideWaiter];
//        [self hideReloadView];
//        [self.tableView.mj_header endRefreshing];
//        [self.tableView.mj_footer endRefreshing];
//        if (success) {
//            [self hideRequestAgainView];
//            self.tableView.mj_footer.hidden = lists.count<20;
//            if (self.curPage == 1) {
//                [self.infomations removeAllObjects];
//            }
//            [self.infomations addObjectsFromArray:lists];
//            [self.tableView reloadData];
//        }else{
//            [MBProgressHUD showError:msg toView:self.view];
//        }
//    }];
}

- (NSMutableArray *)infomations
{
    if (!_infomations) {
        _infomations = [NSMutableArray array];
    }
    return _infomations;
}

@end
